package outer

import (
	"container/list"
	"sync"
	"fmt"
	"gitee.com/thomasgw/gms/common"
)

type Component struct {
	list *list.List
	locker *sync.Mutex
}

func NewComponent() *Component {
	return &Component{
		list: list.New(),
		locker: new(sync.Mutex),
	}
}

func (c *Component) Receive(item interface{}) bool {
	c.locker.Lock()
	defer c.locker.Unlock()
	fmt.Println("[CLIENT] Receive event, counter: ", string(item.(common.ServerEvent).Payload))
	c.list.PushBack(item)
	return true
}

func (c *Component) ListAll() []common.ServerEvent {
	c.locker.Lock()
	defer c.locker.Unlock()
	result := make([]common.ServerEvent, 0)
	for elem := c.list.Front(); elem != c.list.Back(); elem = elem.Next() {
		result = append(result, elem.Value.(common.ServerEvent))
	}
	return result
}
